package views;

import controllers.EventHandler;
import java.awt.Color;
import java.awt.Container;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;

public class ProgressBar extends JWindow implements Runnable {

    public static JProgressBar jpb, epb;
    private final int WIDTH = 300;
    private final int HEIGHT = 150;
    private static Container c;
    private static JLabel generate;
    public static int progress = 0;
    private static Calendar cal;
    private static boolean weekly;
    private static float eventprog = 0;
    private static float eventCount = 0;

    public ProgressBar(int x, int y, Calendar cal, boolean weekly) {

        progress = 0;
        eventprog = 0;
        ProgressBar.cal = cal;
        ProgressBar.weekly = weekly;
        this.setBounds(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
        c = this.getContentPane();
        this.setAlwaysOnTop(true);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(new LineBorder(Color.BLACK));
        generate = new JLabel("Generating Schedule... Please wait");
        generate.setBounds(10, 10, 200, 30);
        epb = new JProgressBar();
        epb.setValue((int)eventprog);
        epb.setStringPainted(true);
        epb.setBounds(10, 50, WIDTH-20, 30);
        jpb = new JProgressBar();
        jpb.setValue(progress);
        jpb.setStringPainted(true);
        jpb.setBounds(10, 100, WIDTH-20, 30);
        panel.add(generate);
        panel.add(epb);
        panel.add(jpb);
        c.add(panel);
        this.setVisible(true);
        Thread t = new Thread(this);
        t.start();

    }

    public static void setProgress(int p) {
        progress = p;
    }

    public static void eventDone(){
        eventprog = eventprog + 1;
    }


    @Override
    public void run() {
        if(weekly){
            eventCount=7;
        }
 else{
            eventCount=1;
        }

                    Thread e = new Thread(new createEvent(cal, weekly));
        e.start();
 

        while (eventprog < eventCount) {
            jpb.setValue((int)(eventprog / eventCount));
        }

        this.dispose();
    }
}

class createEvent implements Runnable {

    private Calendar cal;
    private boolean weekly;

    public createEvent(Calendar cal, boolean weekly) {
        this.cal = cal;
        this.weekly = weekly;
    }

    @Override
    public void run() {
        if (!weekly) {
            new EventHandler(cal);
            mainFrame.updateTable();
        } else {
            Calendar temp = cal;
            for (int i = 0; i < 7; i++) {
                new EventHandler(temp);
                temp.setTimeInMillis(temp.getTimeInMillis() + (1000 * 60 * 60 * 24));
                ProgressBar.eventDone();
            }
            mainFrame.updateTable();
        }
    }
}
